<template>
  <view>
    <view class="flex-row tab-bar ba_f">
      <view class="flex-grow-1">
        <navigator
          :class="state == 1 ? 'active' : ''"
          openType="redirect"
          url="order?state=1"
        >
          <text
            :style="
              'color:' +
              (state == 1 ? color : '') +
              ';border-bottom:' +
              (state == 1 ? 4 : 0) +
              'rpx solid ' +
              color
            "
            >拼团中</text
          >
        </navigator>
      </view>
      <view class="flex-grow-1">
        <navigator
          :class="state == 2 ? 'active' : ''"
          openType="redirect"
          url="order?state=2"
        >
          <text
            :style="
              'color:' +
              (state == 2 ? color : '') +
              ';border-bottom:' +
              (state == 2 ? 4 : 0) +
              'rpx solid ' +
              color
            "
            >拼团成功</text
          >
        </navigator>
      </view>
      <view class="flex-grow-1">
        <navigator
          :class="state == 3 ? 'active' : ''"
          openType="redirect"
          url="order?state=3"
        >
          <text
            :style="
              'color:' +
              (state == 3 ? color : '') +
              ';border-bottom:' +
              (state == 3 ? 3 : 0) +
              'rpx solid ' +
              color
            "
            >拼团失败</text
          >
        </navigator>
      </view>
      <view class="flex-grow-1">
        <navigator
          :class="state == 4 ? 'active' : ''"
          openType="redirect"
          url="order?state=4"
        >
          <text
            :style="
              'color:' +
              (state == 4 ? color : '') +
              ';border-bottom:' +
              (state == 4 ? 3 : 0) +
              'rpx solid ' +
              color
            "
            >单独购</text
          >
        </navigator>
      </view>
    </view>
    <view class="order-list">
      <view class="no-data-tip zwdd" v-if="order_list.length == 0">
        <image
          mode="aspectFit"
          src="https://ymqycc.oss-cn-chengdu.aliyuncs.com/ymqzho2o/static/ymq_canyino2o/pages/image/list.png"
        ></image>
        <view>您暂无相关订单哦~</view>
      </view>
      <view
        class="order_list pa_1 ba_f mar_t"
        v-for="(item, index) in order_list"
        :key="index"
      >
        <view class="order_head pa_0">
          <view class="dis_in">
            <image
              class="dis_in"
              mode="aspectFill"
              src="./static/collage/stores.png"
            ></image>
            <text class="fon_30 dis_in">{{ item.store_name }}</text>
          </view>
          <view class="dis_in t_r" :style="'color:' + color">
            <text class="fon_30 mar_l" v-if="state == 1">组团中</text>
            <text class="fon_30 mar_l" v-if="state == 2">组团成功</text>
            <text class="fon_30 mar_l" v-if="state == 3">组团失败</text>
            <text class="fon_30 mar_l" v-if="state == 4">单独购</text>
          </view>
        </view>

        <view class="shop_info pa_0">
          <view class="dis_in">
            <image mode="aspectFill" :src="item.logo"></image>
          </view>
          <view class="dis_in">
            <view class="fon_30 color_6">{{ item.goods_name }}</view>
            <view class="info pa_0">
              <view class="color_red fon_24 dis_in">￥{{ item.money }}</view>
              <view class="fon_22 t_r dis_in">{{ item.pay_time }}</view>
            </view>
          </view>
        </view>

        <view @tap="order_info" class="pa_0 t_r order_info" :data-info="item">
          <text
            class="fon_30"
            :style="'border:2rpx solid ' + color + ';color:' + color"
            >查看订单</text
          >
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index'
import gettel from '@/ymq_canyino2o/dist/gettel/index'
var app = getApp()
var util = require('../../utils/util.js')
export default {
  components: {
    cardlist,
    gettel
  },
  data() {
    return {
      color: '#34aaff',
      state: 1,
      order_list: [],
      show_no_data_tip: false,
      hide: 1,
      qrcode: '',
      pagenum: 1,
      storelist: [],
      mygd: false,
      jzgd: true,
      System: '',
      hx_code: ''
    }
  },
  onLoad: function (t) {
    uni.setNavigationBarTitle({
      title: '我的拼团'
    })
    app.globalData.setNavigationBarColor(this)
    var that = this
    app.globalData.util.request({
      url: 'entry/wxapp/System',
      cachetime: '0',
      success: function (t) {
        console.log(t)
        var e = t.data
        if ('2' == e.model) {
          getApp().globalData.sjid = e.default_store
        }
        if ('4' == e.model) {
          getApp().globalData.sjid = e.default_store
        }
        if ('' == t.data.dc_name) {
          t.data.dc_name = '店内'
        }
        if ('' == t.data.wm_name) {
          t.data.wm_name = '外卖'
        }
        if ('' == t.data.yd_name) {
          t.data.yd_name = '预定'
        }
        that.setData({
          System: t.data
        })
      }
    })
    console.log(t)
    this.setData({
      state: t.state
    })
    if (4 != t.state) {
      that.reLoad()
    } else {
      that.order()
    }
  },
  onReachBottom: function () {
    console.log('上拉加载', this.pagenum)
    var that = this
    if (4 != that.state) {
      that.reLoad()
    } else {
      that.order()
    }
  },
  methods: {
    reLoad: function () {
      var that = this
      var r = this.state || 1
      var t = uni.getStorageSync('users').id
      var e = this.pagenum
      app.globalData.util.request({
        url: 'entry/wxapp/MyGroupOrder',
        cachetime: '0',
        data: {
          state: r,
          user_id: t,
          page: e
        },
        success: function (t) {
          console.log('分页返回的列表数据', t)
          for (var e = 0; e < t.data.length; e++) {
            t.data[e].state = r
            t.data[e].xf_time = util.ormatDate(t.data[e].xf_time)
            t.data[e].pay_time = util.ormatDate(t.data[e].pay_time)
          }
          if (0 < t.data.length) {
            that.setData({
              jzgd: true,
              pagenum: that.pagenum + 1
            })
          }
          var a = that.storelist
          a = (function (t) {
            for (var e = [], a = 0; a < t.length; a++) {
              if (-1 == e.indexOf(t[a])) {
                e.push(t[a])
              }
            }
            return e
          })((a = a.concat(t.data)))
          that.setData({
            order_list: a,
            storelist: a
          })
          console.log(a)
        }
      })
    },

    order: function () {
      var that = this
      var r = this.state || 1
      var t = uni.getStorageSync('users').id
      var s = this.pagenum
      app.globalData.util.request({
        url: 'entry/wxapp/MyGroupOrder',
        cachetime: '0',
        data: {
          type: 1,
          user_id: t,
          page: s
        },
        success: function (t) {
          console.log('分页返回的列表数据', t)
          for (var e = 0; e < t.data.length; e++) {
            t.data[e].status = r
            t.data[e].xf_time = util.ormatDate(t.data[e].xf_time)
            t.data[e].pay_time = util.ormatDate(t.data[e].pay_time)
            if (
              15 <= t.data[e].receive_address.length &&
              '' != t.data[e].receive_address
            ) {
              t.data[e].receive_address =
                t.data[e].receive_address.slice(0, 15) + '...'
            }
          }
          var a = that.storelist
          a = (function (t) {
            for (var e = [], a = 0; a < t.length; a++) {
              if (-1 == e.indexOf(t[a])) {
                e.push(t[a])
              }
            }
            return e
          })((a = a.concat(t.data)))
          that.setData({
            order_list: a,
            storelist: a,
            pagenum: s + 1
          })
          console.log(a)
        }
      })
    },

    order_info: function (t) {
      var e = t.currentTarget.dataset
      console.log(e.info)
      uni.setStorageSync('order_info', e.info)
      uni.navigateTo({
        url: 'order_info'
      })
    },

    orderPay: function (t) {
      var e = getApp().globalData.getOpenId
      var a = uni.getStorageSync('users').id
      var o = t.currentTarget.dataset.id
      var r = t.currentTarget.dataset.money
      var s = t.currentTarget.dataset.type
      console.log(e, a, o, r, s)
      if ('5' == s) {
        uni.showModal({
          title: '提示',
          content: '您的支付方式为餐后支付，请到收银台付款'
        })
      } else {
        uni.showLoading({
          title: '正在提交',
          mask: true
        })
        app.globalData.util.request({
          url: 'entry/wxapp/QgPay',
          cachetime: '0',
          data: {
            openid: e,
            money: r,
            order_id: o
          },
          success: function (t) {
            console.log(t)
            uni.hideLoading()
            uni.requestPayment({
              timeStamp: t.data.timeStamp,
              nonceStr: t.data.nonceStr,
              package: t.data.package,
              signType: t.data.signType,
              paySign: t.data.paySign,
              success: function (t) {
                console.log(t.data)
              },
              complete: function (t) {
                console.log(t)
                if ('requestPayment:fail cancel' == t.errMsg) {
                  uni.showToast({
                    title: '取消支付',
                    icon: 'loading',
                    duration: 1000
                  })
                }
                if ('requestPayment:ok' == t.errMsg) {
                  uni.showToast({
                    title: '支付成功',
                    duration: 1000
                  })
                  if (1 == s) {
                    setTimeout(function () {
                      uni.redirectTo({
                        url: 'order?status=2'
                      })
                    }, 1000)
                  }
                  if (2 == s) {
                    setTimeout(function () {
                      uni.redirectTo({
                        url: 'order?status=4'
                      })
                    }, 1000)
                  }
                }
              }
            })
          }
        })
      }
    },

    canceldd: function (t) {
      var e = t.currentTarget.dataset.id
      console.log(e)
      uni.showModal({
        title: '提示',
        content: '是否取消该订单？',
        cancelText: '否',
        confirmText: '是',
        success: function (t) {
          if (t.cancel) {
            return true
          }
          if (t.confirm) {
            uni.showLoading({
              title: '操作中'
            })
            app.globalData.util.request({
              url: 'entry/wxapp/CancelOrder',
              cachetime: '0',
              data: {
                order_id: e
              },
              success: function (t) {
                console.log(t.data)
                if ('1' == t.data) {
                  uni.showToast({
                    title: '取消成功',
                    icon: 'success',
                    duration: 1000
                  })
                  setTimeout(function () {
                    uni.redirectTo({
                      url: 'order?status=5'
                    })
                  }, 1000)
                } else {
                  uni.showToast({
                    title: '请重试',
                    icon: 'loading',
                    duration: 1000
                  })
                }
              }
            })
          }
        }
      })
    },

    orderRevoke: function (t) {
      var e = t.currentTarget.dataset.id
      console.log(e)
      uni.showModal({
        title: '提示',
        content: '是否删除该订单？',
        cancelText: '否',
        confirmText: '是',
        success: function (t) {
          if (t.cancel) {
            return true
          }
          if (t.confirm) {
            uni.showLoading({
              title: '操作中'
            })
            app.globalData.util.request({
              url: 'entry/wxapp/DelQgOrder',
              cachetime: '0',
              data: {
                order_id: e
              },
              success: function (t) {
                console.log(t.data)
                if ('1' == t.data) {
                  uni.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 1000
                  })
                  setTimeout(function () {
                    uni.redirectTo({
                      url: 'order?status=3'
                    })
                  }, 1000)
                } else {
                  uni.showToast({
                    title: '请重试',
                    icon: 'loading',
                    duration: 1000
                  })
                }
              }
            })
          }
        }
      })
    },

    txsj: function (t) {
      console.log('提醒商家' + t.currentTarget.dataset.tel)
      uni.makePhoneCall({
        phoneNumber: t.currentTarget.dataset.tel
      })
    },

    sqtk: function (e) {
      console.log('申请退款' + e.currentTarget.dataset.id)
      uni.showModal({
        title: '提示',
        content: '申请退款么',
        success: function (t) {
          if (t.cancel) {
            return true
          }
          if (t.confirm) {
            uni.showLoading({
              title: '操作中'
            })
            app.globalData.util.request({
              url: 'entry/wxapp/TkOrder',
              cachetime: '0',
              data: {
                order_id: e.currentTarget.dataset.id
              },
              success: function (t) {
                console.log(t.data)
                if ('1' == t.data) {
                  uni.showToast({
                    title: '申请成功',
                    icon: 'success',
                    duration: 1000
                  })
                  setTimeout(function () {
                    uni.redirectTo({
                      url: 'order?status=5'
                    })
                  }, 1000)
                } else {
                  uni.showToast({
                    title: '请重试',
                    icon: 'loading',
                    duration: 1000
                  })
                }
              }
            })
          }
        }
      })
    },

    qrsh: function (t) {
      var e = t.currentTarget.dataset.id
      console.log(e)
      uni.showModal({
        title: '提示',
        content: '是否确认已收到货？',
        cancelText: '否',
        confirmText: '是',
        success: function (t) {
          if (t.cancel) {
            return true
          }
          if (t.confirm) {
            uni.showLoading({
              title: '操作中'
            })
            app.globalData.util.request({
              url: 'entry/wxapp/OkOrder',
              cachetime: '0',
              data: {
                order_id: e
              },
              success: function (t) {
                console.log(t.data)
                if ('1' == t.data) {
                  uni.showToast({
                    title: '收货成功',
                    icon: 'success',
                    duration: 1000
                  })
                  setTimeout(function () {
                    uni.redirectTo({
                      url: 'order?status=4'
                    })
                  }, 1000)
                } else {
                  uni.showToast({
                    title: '请重试',
                    icon: 'loading',
                    duration: 1000
                  })
                }
              }
            })
          }
        }
      })
    },

    orderQrcode: function (a) {
      var that = this
      var r = that.order_list
      var s = a.target.dataset.index
      uni.showLoading({
        title: '正在加载',
        mask: true
      })
      if (that.order_list[s].offline_qrcode) {
        that.setData({
          hide: 0,
          qrcode: that.order_list[s].offline_qrcode
        })
        uni.hideLoading()
      } else {
        e.request({
          url: t.order.get_qrcode,
          data: {
            order_no: r[s].order_no
          },
          success: function (t) {
            if (0 == t.code) {
              that.setData({
                hide: 0,
                qrcode: t.data.url
              })
            } else {
              uni.showModal({
                title: '提示',
                content: t.msg
              })
            }
          },
          complete: function () {
            uni.hideLoading()
          }
        })
      }
    },

    hideFun: function (t) {
      this.setData({
        hide: 1
      })
    },

    hxqh: function (t) {
      var that = this
      var a = t.currentTarget.dataset.id
      var o = t.currentTarget.dataset.sjid
      console.log(a, o)
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      app.globalData.util.request({
        url: 'entry/wxapp/OrderCode',
        cachetime: '0',
        data: {
          order_id: a
        },
        success: function (t) {
          console.log(t.data)
          that.setData({
            hx_code: t.data,
            hide: 2
          })
        }
      })
    }
  }
}
</script>
<style>
@import './order.css';
</style>
